package com.xqy.wexmini.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xqy.wexmini.VO.HonorVO;
import com.xqy.wexmini.common.ResponseResult;
import com.xqy.wexmini.domain.*;
import com.xqy.wexmini.form.AnswerForm;
import com.xqy.wexmini.service.AnswerService;
import com.xqy.wexmini.service.HonorService;
import com.xqy.wexmini.service.UserService;
import com.xqy.wexmini.service.WrongService;
import com.xqy.wexmini.utils.JwtTokenUtils;
import com.xqy.wexmini.utils.Newsutils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xia
 * @since 2022-05-30
 */
@Api(tags = "答题")
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    @Autowired
    private WrongService wrongService;
    @Autowired
    private HonorService honorService;
    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Autowired
    private UserService userService;

    @ApiOperation("改变")
    @PostMapping("/nb")
    public void data1(){
        List<Answer> list = answerService.list();
        for(Answer answer:list){
            if(answer.getType()==null) {
                answer.setType("蔬菜");
                answerService.updateById(answer);
            }
        }
        return;
    }

    @ApiOperation("随机返回15条数据")
    @PostMapping("/data")
    public ResponseResult data(){
        List<Answer> list = answerService.list();
        Random r=new Random();
        HashSet<Integer> hashSet=new HashSet();
        while (true) {
            int i = r.nextInt(list.size());
            hashSet.add(i);
            if(hashSet.size()>=15){
                break;
            }
        }
        List<Answer> list1=new ArrayList<>();
        for(int s:hashSet){
            list1.add(list.get(s));
        }
        return new ResponseResult(200,list1);
    }
    @ApiOperation("根据类型返回15条数据")
    @PostMapping("/type")
    public ResponseResult type(String type){
        List<Answer> list = answerService.list(new QueryWrapper<Answer>().lambda()
                .eq(Answer::getType,type)
        );
        Random r=new Random();
        HashSet<Integer> hashSet=new HashSet();
        while (true) {
            int i = r.nextInt(list.size());
            hashSet.add(i);
            if(hashSet.size()>=15){
                break;
            }
        }
        List<Answer> list1=new ArrayList<>();
        for(int s:hashSet){
            list1.add(list.get(s));
        }
        return new ResponseResult(200,list1);
    }
    @ApiOperation("答题完毕返回用户答错的题目数目")
    @PostMapping("/result")
    public ResponseResult result(@RequestBody List<AnswerForm> all, HttpServletRequest httpServletRequest) {
        String username = jwtTokenUtils.getOpenId(httpServletRequest);
        int flag = 0;
        for (AnswerForm answerForm : all) {
            Answer byId = answerService.getById(answerForm.getAnswerId());
            if (byId.getAnswer().equals(answerForm.getAnswer())) {
                ;
            } else {
                Wrong wrong = new Wrong();
                wrong.setAnswerId(answerForm.getAnswerId());
                wrong.setUsername(username);
                wrongService.save(wrong);
                flag++;
            }
        }
        if (flag == 0) {
            List<Honor> honors = honorService.list(new QueryWrapper<Honor>().lambda()
                    .eq(Honor::getUsername, username));
            if (honors.isEmpty()) {
                Honor honor = new Honor();
                honor.setHonor(1);
                honor.setUsername(username);
                honorService.save(honor);
            } else {
                Honor honor = honors.get(0);
                Integer honor1 = honor.getHonor();
                honor.setHonor(++honor1);
                honorService.updateById(honor);
            }
            return new ResponseResult(200, 0);
        } else {
            return new ResponseResult(200, flag);
        }
    }
    @ApiOperation("查看错题集")
    @PostMapping("/wrong")
    public ResponseResult wrong(HttpServletRequest httpServletRequest){
        String username = jwtTokenUtils.getOpenId(httpServletRequest);
        List<Wrong> list = wrongService.list(new LambdaQueryWrapper<Wrong>()
                .eq(Wrong::getUsername, username)
        );
        List<Answer> answers=new ArrayList<>();
        for(Wrong wrong:list){
            Answer byId = answerService.getById(wrong.getAnswerId());
            answers.add(byId);;
        }
        return new ResponseResult(200,answers);
    }
    @ApiOperation("做错题集")
    @PostMapping("/wrong1")
    public ResponseResult wrong1(List<AnswerForm> all,HttpServletRequest httpServletRequest){
        String username = jwtTokenUtils.getOpenId(httpServletRequest);
        int flag=0;
        for (AnswerForm answerForm : all) {
            Answer byId = answerService.getById(answerForm.getAnswerId());
            if (byId.getAnswer().equals(answerForm.getAnswer())) {
                List<Wrong> list = wrongService.list(new QueryWrapper<Wrong>().lambda()
                        .eq(Wrong::getAnswerId, answerForm.getAnswerId())
                );
                for(Wrong wrong:list){
                    wrongService.removeById(wrong.getId());
                }
            } else {
               flag++;
            }
        }
        return new ResponseResult(200,flag);
    }
    @ApiOperation("查看荣誉值")
    @PostMapping("/honor")
    public ResponseResult honor(HttpServletRequest httpServletRequest){
        String username = jwtTokenUtils.getOpenId(httpServletRequest);
        Honor one = honorService.getOne(new QueryWrapper<Honor>().lambda()
                .eq(Honor::getUsername, username)
        );
        if(one==null){
            return new ResponseResult(200,0);
        }else {
            return new ResponseResult(200,one.getHonor());
        }
    }
    @ApiOperation("查看排行榜")
    @PostMapping("/see1")
    public ResponseResult see1(){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByAsc("honor");
        List<Honor> honor = honorService.list(queryWrapper);
        List<HonorVO> honors=new ArrayList<>();
        int a=0;
        HonorVO honorVO=new HonorVO();
        for(Honor honor1:honor){
            List<User> users = userService.selectList(new QueryWrapper<User>().lambda()
                    .eq(User::getOpenId, honor1.getUsername()));
            honor1.setUsername(users.get(0).getOpenId());
            BeanUtils.copyProperties(honor1,honorVO);
            honorVO.setNickName(users.get(0).getUserName());
            honorVO.setAvatarUrl(users.get(0).getAvatarUrl());
            honors.add(honorVO);
           if(a==9){
               break;
           }
        }
        return new ResponseResult(200,honors);
    }
}
